﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UtilLib;

namespace UtilWpf
{
    /// <summary>
    /// UserControlTimePicker.xaml 的交互逻辑
    /// </summary>
    public partial class AucTimePicker : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public AucTimePicker()
        {
            InitializeComponent();

            TextBoxHour.Text = "0";
            TextBoxMinute.Text = "0";
            TextBoxSecond.Text = "0";
        }
        private void TextBoxHour_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
        }
        private void TextBoxMinute_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
        }
        private void TextBoxSecond_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            Regex re = new Regex("[^0-9.-]+");
            e.Handled = re.IsMatch(e.Text);
            if (e.Handled)
            {
                var value = e.Text.ToInt();
                e.Handled = value >= 0 && value <= 59;
            }
        }
        private void TextBoxHour_TextChanged(object sender, TextChangedEventArgs e)
        {
            var value = TextBoxHour.Text.ToInt();
            if (value < 0) { value = 0; }
            if (value > 23) { value = 23; }
            TextBoxHour.Text = value.ToString();
        }
        private void TextBoxMinute_TextChanged(object sender, TextChangedEventArgs e)
        {
            var value = TextBoxMinute.Text.ToInt();
            if (value < 0) { value = 0; }
            if (value > 59) { value = 59; }
            TextBoxMinute.Text = value.ToString();
        }
        private void TextBoxSecond_TextChanged(object sender, TextChangedEventArgs e)
        {
            var value = TextBoxSecond.Text.ToInt();
            if (value < 0) { value = 0; }
            if (value > 59) { value = 59; }
            TextBoxSecond.Text = value.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        public int ValueHour { get { return TextBoxHour.Text.ToInt(); } }
        /// <summary>
        /// 
        /// </summary>
        public int ValueMinute { get { return TextBoxMinute.Text.ToInt(); } }
        /// <summary>
        /// 
        /// </summary>
        public int ValueSecond { get { return TextBoxSecond.Text.ToInt(); } }
        /// <summary>
        /// 
        /// </summary>
        public (int, int, int) ValueTime { get { return (TextBoxHour.Text.ToInt(), TextBoxMinute.Text.ToInt(), TextBoxSecond.Text.ToInt()); } }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="h"></param>
        /// <param name="m"></param>
        /// <param name="s"></param>

        public void SetTime(int h, int m, int s)
        {
            TextBoxHour.Text = h.ToString();
            TextBoxMinute.Text = m.ToString();
            TextBoxSecond.Text = s.ToString();

        }
    }
}
